<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改号源信息</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-5">
    <h2>修改号源信息</h2>
    <form id="updateSourceForm">
        <input type="hidden" id="id" name="id">
        <div class="mb-3">
            <label for="doctorId" class="form-label">医生ID</label>
            <input type="number" class="form-control" id="doctorId" name="doctorId" required>
        </div>
        <div class="mb-3">
            <label for="date" class="form-label">日期</label>
            <input type="date" class="form-control" id="date" name="date" required>
        </div>
        <div class="mb-3">
            <label for="period" class="form-label">时段</label>
            <select class="form-select" id="period" name="period" required>
                <option value="1">上午</option>
                <option value="2">下午</option>
            </select>
        </div>
        <div class="mb-3">
            <label for="totalNumber" class="form-label">总号源数量</label>
            <input type="number" class="form-control" id="totalNumber" name="totalNumber" required>
        </div>
        <div class="mb-3">
            <label for="availableNumber" class="form-label">可用号源数量</label>
            <input type="number" class="form-control" id="availableNumber" name="availableNumber" required>
        </div>
        <div class="mb-3">
            <label for="price" class="form-label">挂号费用</label>
            <input type="number" step="0.01" class="form-control" id="price" name="price" required>
        </div>
        <div class="mb-3">
            <label for="status" class="form-label">状态</label>
            <select class="form-select" id="status" name="status">
                <option value="1">可用</option>
                <option value="0">不可用</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">保存修改</button>
    </form>
</div>

<script src="/js/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        // 修复后的日期格式化函数
        function formatDateForInput(dateString) {
            if (!dateString) return '';
            try {
                // 处理各种日期格式
                const date = new Date(dateString);
                if (isNaN(date)) {
                    console.warn("无效日期:", dateString);
                    return '';
                }

                // 转换为本地时区的YYYY-MM-DD格式
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');

                return `${year}-${month}-${day}`;
            } catch (e) {
                console.error("日期处理错误:", e);
                return '';
            }
        }

        const urlParams = new URLSearchParams(window.location.search);
        const scheduleId = urlParams.get('id');

        if (!scheduleId) {
            alert("缺少号源ID");
            return;
        }

        // 获取号源详情
        $.get("/admins/getScheduleById/" + scheduleId, function (schedule) {
            if (!schedule) {
                alert("未找到号源");
                return;
            }

            // 填充表单数据
            $('#id').val(schedule.id);
            $('#doctorId').val(schedule.doctorId);
            $('#date').val(formatDateForInput(schedule.date)); // 使用修复后的格式化
            $('#period').val(schedule.period);
            $('#totalNumber').val(schedule.totalNumber);
            $('#availableNumber').val(schedule.availableNumber);
            $('#price').val(schedule.price);
            $('#status').val(schedule.status);
        }).fail(function(jqXHR) {
            alert("获取失败: " + jqXHR.responseJSON?.message || "服务器错误");
        });

        // 表单提交处理
        $('#updateSourceForm').on('submit', function (e) {
            e.preventDefault();

            const rawDate = $('#date').val();
            console.log("原始日期值:", rawDate);  // 调试日志

            // 日期非空验证
            if (!$('#date').val()) {
                alert("日期不能为空！");
                return;
            }

            // 确保日期格式正确
            const formattedDate = formatDateForInput(rawDate);
            if (!formattedDate) {
                alert("无效的日期格式，请使用YYYY-MM-DD格式");
                return;
            }
            console.log("格式化后的日期值:", formattedDate);

            const updatedSource = {
                id: parseInt($('#id').val()),
                doctorId: parseInt($('#doctorId').val()),
                scheduleDate: formattedDate,  // 使用格式化后的日期
                period: parseInt($('#period').val()),
                totalNumber: parseInt($('#totalNumber').val()),
                availableNumber: parseInt($('#availableNumber').val()),
                price: parseFloat($('#price').val()),
                status: parseInt($('#status').val())
            };

            console.log("完整请求数据:", JSON.stringify(updatedSource, null, 2));  // 调试日志

            $.ajax({
                url: "/admins/updateSchdule",
                type: "PUT",
                contentType: "application/json",
                data: JSON.stringify(updatedSource),
                success: function () {
                    alert("更新成功");
                    window.location.href = "/scheduleList";
                },
                error: function (jqXHR) {
                    alert("更新失败: " + (jqXHR.responseJSON?.message || "服务器错误"));
                }
            });
        });
    });
</script>

</body>
</html>